import sys

def output(word, llinks, rlinks, lsharp, rsharp):
    if not word:
        return
    write = sys.stdout.write
    write(word + '\t')
    if lsharp:
        write('< ')
    if rsharp:
        write('> ')
    write('degl:%d ' % len(llinks))
    write('degr:%d ' % len(rlinks))
    for w in llinks:
        write(' l:%s' % w)
    for w in rlinks:
        write(' r:%s' % w)
    write('\n')

def main():
    '''
    NOTE: no mapper for this job
    input: links_third.txt
    output: links.txt
    (word, {'<', '>', 'l:'linkee.., 'r:'linkee..}) -> 
        (word, '<' '>' 'degl:'degl 'degr:'degr 'l:'linkee.. 'r:'linkee..)
    '''
    prev_word = ''
    llinks = []
    rlinks = []
    lsharp = False
    rsharp = False
    for line in sys.stdin:
        line = line.strip()
        word, rest = line.split()
        if word != prev_word:
            output(prev_word, llinks, rlinks, lsharp, rsharp)
            llinks = []
            rlinks = []
            lsharp = False
            rsharp = False
            prev_word = word
        if rest == 'x':
            pass
        elif rest == '<':
            lsharp = True
        elif rest == '>':
            rsharp = True
        elif rest.startswith('l:'):
            llinks.append(rest[2: ])
        elif rest.startswith('r:'):
            rlinks.append(rest[2: ])
        else:
            raise RuntimeError('invalid key-value pair: %s, %s' % (word, rest))
    output(prev_word, llinks, rlinks, lsharp, rsharp)

if __name__ == '__main__':
    main()
